package com.g13.virtualpethospital.service.Implement;

import com.g13.virtualpethospital.entity.CaseEntity;
import com.g13.virtualpethospital.entity.TestExamEntity;
import com.g13.virtualpethospital.exceptions.TestExamException;
import com.g13.virtualpethospital.service.TestExamService;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Title: TestExamServiceImpl
 * @Author yyj1205
 * @Package com.g13.virtualpethospital.service.Implement
 * @Date 24/3/2024 23:13
 * @description: QuestionService的一个实现
 */
@Service
public class TestExamServiceImpl implements TestExamService {
    @Autowired
    MongoTemplate mongoTemplate;
    @Override
    public void addTestExam(TestExamEntity testExam) {
        testExam.setTestExamId(null);
        mongoTemplate.insert(testExam);
    }

    @Override
    public void updateTestExam(TestExamEntity testExam, String id) throws TestExamException {
        TestExamEntity testExamEntity = mongoTemplate.findById(id, TestExamEntity.class);
        if(testExamEntity == null) throw new TestExamException(TestExamException.TestExamErrorCode.NO_TESTEXAM);
        Query query = new Query(Criteria.where("_id").is(id));
        Update update = new Update();
        if (testExam.getTestId() != null)
        update.set("testId", testExam.getTestId());
        if(testExam.getUserId() != null)
        update.set("userId", testExam.getUserId());
        if (testExam.getAnswers() != null)
        update.set("answers", testExam.getAnswers());
//        if (testExam.getScore() != null)
        update.set("score", testExam.getScore());
        UpdateResult updateResult = mongoTemplate.upsert(query,update,TestExamEntity.class);
    }

    @Override
    public void deleteTestExam(String id) throws TestExamException {
        TestExamEntity testExamEntity = mongoTemplate.findById(id, TestExamEntity.class);
        if(testExamEntity == null) throw new TestExamException(TestExamException.TestExamErrorCode.NO_TESTEXAM);
        Query query = new Query(Criteria.where("_id").is(id));
        DeleteResult deleteResult = mongoTemplate.remove(query, TestExamEntity.class);
        if(deleteResult.getDeletedCount() == 0) throw new TestExamException(TestExamException.TestExamErrorCode.DEL_FAIL);

    }

    @Override
    public List<TestExamEntity> findAllTestExam() {
        return mongoTemplate.findAll(TestExamEntity.class);
    }

    @Override
    public List<TestExamEntity> findTestExamById(String id) {
        List<TestExamEntity> testExamEntities = new ArrayList<>();
        TestExamEntity testExam = mongoTemplate.findById(id, TestExamEntity.class);
        if (testExam != null) testExamEntities.add(testExam);
        return testExamEntities;

    }

    @Override
    public List<TestExamEntity> findTestExamByUserId(String userId) {
        if(Objects.equals(userId, "") || userId == null) return findAllTestExam();
        Query query = new Query(Criteria.where("userId").is(userId));
        return mongoTemplate.find(query, TestExamEntity.class);
    }

    @Override
    public List<TestExamEntity> findTestExamByPaperId(String paperId) {
        if (paperId == null || paperId == "") return findAllTestExam();
        Query query = new Query(Criteria.where("testId").is(paperId));
        return mongoTemplate.find(query, TestExamEntity.class);
    }
}
